/**
 * Created by wenguangjia on 2017/4/25.
 */
(function () {
    'use strict';
    angular
        .module('logcenterApp')
        .controller('deployController', deployController)
    deployController.$inject = ['$scope',  'searchService', 'toaster'];
    function deployController($scope, searchService, toaster) {
        var notify = function (type, title, message) {
            toaster.pop(type, title, message);
        };

        $scope.param = {title: null, part:"",state: "", archive:false,submitDate: getToday00Clock(), submitDateEnd: getToday11Clock(),module:null,type:""};
        $scope.query = function () {
            $scope.publishCodeGridOptions.dataSource.read();
        };
        $scope.clear = function () {
            $scope.param = {title: null, part:"",state: "", archive:false,submitDate: getToday00Clock(), submitDateEnd: getToday11Clock(),module:null,type:""};
        };

        /**
         * grid
         * @type {*[]}
         */
        $scope.state = [{text:"待上线",value:0},{text:"已上线",value:1},{text:"上线失败",value:-1}];
        $scope.type = [
            {text: "批作业上线", value: 1},{text: "DLL上线", value: 2},{text: "函数上线", value: 3},
            {text: "头文件(3.0)上线", value: 4}, {text: "O文件上线", value: 5},
            {text: "Server上线", value: 6},{text: "Server重启", value: 7},
            {text: "前台特殊文件上线", value: 8}, {text: "前台特殊文件删除", value: 9},
            {text: "表结构上线", value: 10}
            ];
        var columns = [
            {field: "id", title: "主键",hidden:true},
            {field: "title", title: "标题"},
            {field: "description", title: "描述",hidden:true},
            {field: "module", title: "模块",width:'80px'},
            {field: "type", title: "类型",values: $scope.type},
            {field: "location", title: "位置",hidden:true},
            {field: "location", title: "位置",hidden:true},
            {field: "part", title: "分区"},
            {field: "createUser", title: "提交人", hidden: true},
            {field: "submitDate", title: "提交时间", format: "{0: yyyy-MM-dd HH:mm:ss}"},
            {field: "state", title: "状态", values:$scope.state},
            {field: "priority", title: "优先级", hidden: true},
            {field: "explanation", title: "说明", hidden: true},
            {field: "finishDate", title: "关闭时间", format: "{0: yyyy-MM-dd HH:mm:ss}"}
        ];


        $scope.publishCodeGridOptions = {
            selectable: "row",
            scrollable: true,
            sortable: true,
            resizable: true,
            columns: columns,
            columnMenu: true,
            pageable: {
                refresh: true,
                pageSizes: true,
                buttonCount: 5
            }
           // toolbar: [
          //      "create",
                /* {
                 name: "customEdit",
                 text: "编辑",
                 imageClass: "k-edit",
                 className: "k-custom-edit",
                 iconClass: "k-icon"
                 },
                 {
                 name: "customDestroy",
                 text: "删除",
                 imageClass: "k-delete",
                 className: "k-custom-delete",
                 iconClass: "k-icon"
                 },*/
         //   ],
         //    editable: {
         //        mode: "popup",
         //        template: kendo.template($("#grideditor").html())
         //    },
        };
        $scope.publishCodeModel = kendo.data.Model.define({
            id: "id",
            fields: {
//        	id:{type: 'number'},
                title: {validation: {required: true}, type: 'string'},
                submitDate: {type: 'date'},
                finishDate: {type: 'date'},
            }
        });


        $scope.publishCodeGridOptions.dataSource = new kendo.data.DataSource({
            autoBind: false,
            transport: {
                read: function (e) {
                    searchService.queryDeploy($scope.param).then(
                        function (resp) {
                            e.success(resp);
                        }, function (error) {
                            console.log(error);
                            e.error(error);
                            notify('error', '失败', error.message);
                        });
                },
                // create: function (e) {
                //     var model = e.data.models[0];
                //     model.finishDate = null;
                //     deployService.create(model).then(
                //         function (resp) {
                //             // console.log(resp);
                //             // resp.submitDate=getDate(resp.submitDate);
                //             e.success(resp);
                //             notify('success', '成功', '添加成功');
                //         }, function (error) {
                //             console.log(error);
                //             e.error(error);
                //             notify('error', '失败', error.message);
                //         });
                // },
                parameterMap: function (options, operation) {
                    if (operation !== "read" && options.models) {
                        return {models: kendo.stringify(options.models)};
                    }
                }
            },
            batch: true,
            pageSize: 20,
            schema: {
                model: $scope.publishCodeModel
            }
        });


        /**
         * get partition
         * @type {string}
         */
        $scope.typeOptions = {
            template: '<span class="k-state-default">{{dataItem.part}}</span>'
        };
        $scope.typeOptions.dataSource = new kendo.data.DataSource({
            transport: {
                read: function (e) {
                    searchService.getPart().then(
                        function (resp) {
                            var parts = [];
                            angular.forEach(resp.data,function(item){
                                parts.push({part:item});
                            });
                            parts.unshift({part:""});
                            e.success(parts);
                        }, function (err) {
                        }
                    );
                }
            },
        });
        $scope.selectShow = function (item) {
            alert($scope.param.part);
        };
    }
})();
